 //
//  TJWelcomeViewController.swift
//  WeiBo
//
//  Created by TanJian on 15/12/8.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit
import SDWebImage

class TJWelcomeViewController: UIViewController {

    //懒加载头像、背景图和欢迎词
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView:UIImageView = {
        
        let headIcon = UIImageView(image: UIImage(named: "avatar_default_big"))

        let url = NSURL(string: (TJUserAcountViewModel.shareViewModel.userAccount?.avatar_large)!)

        headIcon.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))

        headIcon.layer.cornerRadius = 45
        headIcon.clipsToBounds = true
        
        return headIcon
    }()
    
    private lazy var welcomeLable:UILabel = {
       
        let textLable = UILabel()
        textLable.text = "欢迎健帅回来"
        
        return textLable
        
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }


    func setupUI(){
        
        view = bgImageView
        view.addSubview(iconView)
        view.addSubview(welcomeLable)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view.snp_top).offset(300)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        iconView.alpha = 0
        
        welcomeLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(15)
        }

    }

    //页面加载完成后再执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints(closure: { (make) -> Void in
            
            make.top.equalTo(view.snp_top).offset(120)
            
        })
        iconView.alpha = 1
        welcomeLable.alpha = 0
        
        UIView.animateWithDuration(1.5, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                   self.welcomeLable.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(TJSwitchController, object: self)
                        
                })
        }
        
        
        
        
    }

}







